package com.flyco.modules.stm.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.flyco.modules.oms.entity.OmsOrder;
import com.flyco.modules.oms.entity.StmJdSendOrder;
import com.flyco.modules.stm.entity.StmBizInoutRelation;
import com.flyco.modules.stm.entity.StmStoreInfo;
import com.flyco.modules.stm.mapper.StmBizInoutRelationMapper;
import com.flyco.modules.stm.model.StmStockInoutExcel;
import com.flyco.modules.stm.service.IStmBizInoutRelationService;
import com.flyco.modules.stm.service.IStmOrderTraceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Description: 业务和出入库关联表
 * @Author: flyco
 * @Date: 2022-11-29
 * @Version: V1.0
 */
@DS("store")
@Service
public class StmBizInoutRelationServiceImpl extends ServiceImpl<StmBizInoutRelationMapper, StmBizInoutRelation> implements IStmBizInoutRelationService {

    @Autowired
    private StmBizInoutRelationMapper inoutRelationMapper;
    @Autowired
    private IStmOrderTraceService orderTraceService;

    @Override
    public List<StmStockInoutExcel> getInoutItemRecord(Long inOutId) {
        return inoutRelationMapper.getInoutItemRecord(inOutId);
    }

    @Override
    public void InsertBySendOrderList(OmsOrder order, List<StmJdSendOrder> sendOrderList) {

        List<StmBizInoutRelation> bizInoutRelationList = new ArrayList<>();
        for (StmJdSendOrder jdSendOrder : sendOrderList) {
            StmBizInoutRelation inoutRelation = getBySendOrderId(jdSendOrder.getOrderNo());
            if (inoutRelation == null) {
                inoutRelation = new StmBizInoutRelation();
                //旺店通一个发货单，江心这边插入一条出库单
                inoutRelation.setPartnerId(0L);
                inoutRelation.setStoreId(0L);
                inoutRelation.setStoreName("总仓");
                inoutRelation.setSourceStoreId(order.getStoreId());
                inoutRelation.setSourceStoreName(order.getCompanyName());
                inoutRelation.setStatus(1);
                inoutRelation.setType("STOCK_OUT");
                inoutRelation.setStockBizType("STOCK_OUT_TYPE_SALE");
                inoutRelation.setSourceType("RECEIPT_TYPE_SALE");
                inoutRelation.setSourceId(Long.valueOf(order.getId()));
                inoutRelation.setOrderId(order.getId());
                inoutRelation.setSendOrderId(jdSendOrder.getOrderNo());
                inoutRelation.setCancelFlag(0);
                inoutRelation.setCreateBy("system");
                inoutRelation.setCreateTime(new Date());
                bizInoutRelationList.add(inoutRelation);
            }
        }
        saveBatch(bizInoutRelationList);
    }

    public StmBizInoutRelation getBySendOrderId(String sendOrderId) {

        LambdaQueryWrapper<StmBizInoutRelation> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StmBizInoutRelation::getSendOrderId, sendOrderId);

        List<StmBizInoutRelation> relationList = list(wrapper);

        if (CollectionUtils.isEmpty(relationList)) {
            return null;
        } else {
            return relationList.get(0);
        }
    }

    @Override
    public void cancelByJdSendOrderList(List<StmJdSendOrder> sendOrderList) {

        List<StmBizInoutRelation> bizInoutRelationList = new ArrayList<>();
        for (StmJdSendOrder jdSendOrder : sendOrderList) {

            StmBizInoutRelation inoutRelation = getBySendOrderId(jdSendOrder.getOrderNo());
            if (inoutRelation != null && inoutRelation.getCancelFlag().equals(0)) {
                //需要把出库单废除
                inoutRelation.setCancelFlag(1);
                inoutRelation.setUpdateBy("system");
                inoutRelation.setUpdateTime(new Date());
                bizInoutRelationList.add(inoutRelation);
                //删除订单出库轨迹
                orderTraceService.deleteOrderTrace(jdSendOrder.getLogisticsNo());
            }
            if (!CollectionUtils.isEmpty(bizInoutRelationList)) {
                updateBatchById(bizInoutRelationList);
            }
        }

    }

    @Override
    public List<StmBizInoutRelation> getByOrderId(String orderId) {

        LambdaQueryWrapper<StmBizInoutRelation> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StmBizInoutRelation::getSourceId, orderId)
                .in(StmBizInoutRelation::getStatus, Arrays.asList(5, 7))
                .eq(StmBizInoutRelation::getCancelFlag, 0);

        return list(wrapper);
    }

}
